<%@ page contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.User"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%@page import="dbexp.connection.DbConnection"%>
<%@page import="org.json.JSONObject"%>
<%
DbUser u = (DbUser)session.getAttribute("USER");
String name = request.getParameter("name");
String profile = request.getParameter("profile");
String pass = request.getParameter("pass");
String expire = request.getParameter("expire");
String tablespace = request.getParameter("tablespace");
String tmp_tablespace = request.getParameter("tmp_tablespace");
String lock = request.getParameter("lock");
String roles = request.getParameter("roles");
String sys_privs = request.getParameter("sys_privs");
String action = request.getParameter("action");
boolean brak = (name==null || name.trim().equals("null"));
User ob =  new User(name);
if(!brak && action.equals("create")||action.equals("update")){
	ob.setProfile(profile);
	if(pass!=null){
		ob.setPassword(pass);
	}
	if(expire!=null){
		ob.setExpired(true);
	}
	if(lock!=null){
		if(lock.equals("lock")){
			ob.setLocked(true);
		}else{
			ob.setOpen(true);
		}
	}
	if(roles!=null){
		String[] _roles=roles.split(",");
		for(int i=0;i<_roles.length;i++){
			if(_roles[i].trim().length()>0){
				ob.addRole(_roles[i]);
			}
		}
	}
	if(sys_privs!=null){
		String[] _sys_privs=sys_privs.split(",");
		for(int i=0;i<_sys_privs.length;i++){
			if(_sys_privs[i].trim().length()>0){
				ob.addRole(_sys_privs[i]);
			}
		}
	}
	ob.setDefaultTableSpace(tablespace);
	ob.setTemporaryTableSpace(tmp_tablespace);
	int operate = DbConnection.CREATE;
	if(action.equals("update")){
		operate = DbConnection.ALTER;
	}
	int status = DbMetaData.operateObject(u,ob,operate);
}
	ob = DbMetaData.getUser(u, name);
out.clearBuffer();%><%=new JSONObject().put("success", true).
													put("data",new JSONObject().
															put("name", ob.getName()).
															put("def_tablespace",ob.getDefaultTablespace()).
															put("tmp_tablespace",ob.getTemporaryTablespace()).
															put("profile",ob.getProfile()))%>